package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.car.actionform.CarInnerRegisterActionForm;
import com.tianqu.tms.www.car.controller.CarInnerRegisterConfirmController;
import com.tianqu.tms.www.car.viewhelper.CarInnerRegisterViewhelper;

/**
 * @author zhangxueliang 入库登记 - 信息查看action
 */
public class CarInnerRegisterConfirmAction extends TmsAction {
	private Logger logger = Logger.getLogger(CarInnerRegisterConfirmAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarInnerRegisterActionForm registerForm = (CarInnerRegisterActionForm) uc.getInputForm();
		
		
		if(StringUtil.isNullOrBlank(registerForm.getId())){
			if (StringUtil.isNullOrBlank(registerForm.getVin()) && StringUtil.isNullOrBlank(registerForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				return mapping.getInputForward();
			}

			if (!StringUtil.isNullOrBlank(registerForm.getVin()) && !StringUtil.isNullOrBlank(registerForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				return mapping.getInputForward();
			}

			//车牌5位check
			if (!StringUtil.isNullOrBlank(registerForm.getLicensePlate()) && registerForm.getLicensePlate().length() !=5) {
				setErrorsToRequest(request, TmsConstants.U8009, null); //请输入车牌号码后5位。
				return mapping.getInputForward();
			}
			//vin 6位check
			if (!StringUtil.isNullOrBlank(registerForm.getVin()) && registerForm.getVin().length() !=6) {
				setErrorsToRequest(request, TmsConstants.U8010, null); //请输入车架号后6位。
				return mapping.getInputForward();
			}
		}
		
		//
		CarInnerRegisterConfirmController controller = new CarInnerRegisterConfirmController();
		controller.execute(uc);
		
		CarInnerRegisterViewhelper viewhelper = new CarInnerRegisterViewhelper();
		viewhelper = (CarInnerRegisterViewhelper) uc.getAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		if (viewhelper.getConfirmFlag() == 1) {
			setErrorsToRequest(request, TmsConstants.U0039, null);//查询的结果不存在。			
			return mapping.getInputForward();
		}else if(viewhelper.getConfirmFlag() == 2){			 
			 mapping.findForward(TmsConstants.SUCCESS);
		}else if(viewhelper.getConfirmFlag() == 3){
			return mapping.getInputForward();
		}else{
			return mapping.getInputForward();
		}
 
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
